package com.ws.generation.util;

import cn.hutool.core.util.StrUtil;
import com.ws.generation.entity.Config;

import java.io.*;


public class FileUtils {
	public static void createDir(String dir) {
		File file = new File(dir);
        // 现在创建目录
        if(!file.exists()) { //判断文件或文件夹是否存在
        	file.mkdirs();
        }
	}
	public static void deleteFile(String dir) {
		File file = new File(dir);
        file.delete();
	}
	
	public static void read() {
		try {
			FileInputStream fis=new FileInputStream("template/UserMapper.java");
			//（fis,10000）10000为缓冲区大小，大小要在不断试验中调整大小，不是越大越好
			BufferedInputStream bis=new BufferedInputStream(fis,1024*1024);  
			
			byte input[]=new byte[1024*1024];  //每次读取100字节，大小要在不断试验中调整大小，不是越大越好
			int count=0;       
			System.out.println(123);
			//记录读取次数
			while(bis.read(input)!=-1){
				count++;
				String res = new String(input,"UTF-8");
				for(int i=0;i<100;i++)
				System.out.println(res);
				
			}
			bis.close();                                                                    //后打开的要先关闭
			fis.close();

		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
	
	//通过路径读取字符串
	public static String readString(String fileDir) {
		String s="";
		StringBuffer res=new StringBuffer();
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(fileDir), "UTF-8");
			//（fis,10000）10000为缓冲区大小，大小要在不断试验中调整大小，不是越大越好
			BufferedReader bis=new BufferedReader(isr,1024*1024*8);  
			
			//读取文件
			while( (s=bis.readLine() )!=null){
				res.append(s);
				res.append("\n");
			} 
			//后打开的要先关闭
			isr.close();   
			bis.close();     

		} catch (IOException e) {
			e.printStackTrace();
		}
		return res.toString();
	}
	
	//通过路径写字符串
	public static void writeString(String mapperString,String dir,String fileName) {
		// TODO Auto-generated method stub
		//创建路径
		FileUtils.createDir(dir);
		FileUtils.deleteFile(dir+"/"+fileName);
        try {
			OutputStreamWriter isr = new OutputStreamWriter(new FileOutputStream(dir+"/"+fileName), "UTF-8");
			BufferedWriter bw=new BufferedWriter(isr, 1024*1024*8);
			bw.write(mapperString);
			
			bw.flush();
			
			isr.close();
			bw.close();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("项目输出错误！！！！");
		}
	}

	
	public static Config getConfig(String configString) {
		// TODO Auto-generated method stub
		String []s=configString.split("\n");
		
		Config config=new Config();
		for(String str : s) {
			if(StrUtil.isNotBlank(str)) {
				if(str.charAt(0)=='#') {
					continue;
				}
				String filed=str.split(":")[0];
				String val=str.split(":")[1];
				
				if(filed.equals("author")) {
					config.setAuthor(val);
				}else if(filed.equals("packageName")) {
					config.setPackageName(val);
					config.setDirName(val.replaceAll("\\.", "/"));
				}else if(filed.equals("tableName")) {
					config.setTableName(val);
					String javaTableNam=StringUtils.camel(val);
					config.setJavaTableName(javaTableNam);
					config.setUpperJavaTableName(javaTableNam.substring(0, 1).toUpperCase()+javaTableNam.substring(1));
				}else if(filed.equals("controllerDirPre")){
					config.setControllerDirPre(val);
				}
			}
		}
		return config;
	}
}
